{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Pythonについて"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 変数の代入はオブジェクトにラベルを貼るようなイメージ"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4405760432\n",
      "4405760432\n",
      "4405760496\n",
      "4405760432\n"
     ]
    }
   ],
   "source": [
    "### 数値の「1」が入ったオブジェクトが作られると同時に、そのオブジェクトにaというラベルがつく\n",
    "a = 1\n",
    "print(id(a))\n",
    "\n",
    "### 数値の「1」が入ったオブジェクトにbというラベルも貼られる\n",
    "b = a\n",
    "\n",
    "### aとbは同じオブジェクト\n",
    "print(id(b))\n",
    "\n",
    "### 数値の「3」が入ったオブジェクトにaというラベルを張り替える\n",
    "a = 3\n",
    "print(id(a))\n",
    "\n",
    "### bというラベルは「1」のオブジェクトに貼られたまま\n",
    "print(id(b))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## lambda式\n",
    "\n",
    "* lamda式\n",
    "    * 無名関数を表現する記法の一つ\n",
    "    * map関数でよく使われる"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "3\n",
      "[2, 4, 6]\n"
     ]
    }
   ],
   "source": [
    "### 普通の関数で足し算\n",
    "def plus(x, y):\n",
    "    return x + y\n",
    "print(plus(1,2))\n",
    "\n",
    "### lambda式で足し算\n",
    "print((lambda x, y: x + y)(1,2))\n",
    "\n",
    "### map関数での利用例\n",
    "a = [1,2,3]\n",
    "print(list(map(lambda x: x * 2, a)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 4, 6]"
     ]
    }
   ],
   "source": [
    "%%bash\n",
    "\n",
    "### Ruby\n",
    "ruby -e \"\n",
    "a = [1,2,3]\n",
    "print a.map {|x| x*2}\n",
    "\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## リスト中に含まれる文字列を変換する\n",
    "\n",
    "* GeoJSONにするために、wktからPointと()を削除して二つの数値をタプルに入れたい"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'POINT (134.525319 34.079265)'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "geolist = 'POINT (134.525319 34.079265)'\n",
    "geolist"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### replaceでPointと()を削除する"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'134.525319 34.079265'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "geolist.replace('POINT (','').replace(')','')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### リストに入れる"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['134.525319', '34.079265']"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "geolist.replace('POINT (','').replace(')','').split(\" \")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### リストの要素を数値に変換する"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[134.525319, 34.079265]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(map(float, geolist.replace('POINT (','').replace(')','').split(\" \")))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### リストをタプルに変換する"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(134.525319, 34.079265)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = tuple(list(map(float, geolist.replace('POINT (','').replace(')','').split(\" \"))))\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{\"coordinates\": [134.525319, 34.079265], \"type\": \"Point\"}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from geojson import Point\n",
    "Point(data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## リストに要素を追加する"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = [1,2,3]\n",
    "a.append(4)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'list' object has no attribute 'replace'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-18-e37a31833d00>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtuple\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlist\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmap\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfloat\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mgeolist\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreplace\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'POINT ('\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m''\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreplace\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m')'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m''\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msplit\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\" \"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mAttributeError\u001b[0m: 'list' object has no attribute 'replace'"
     ]
    }
   ],
   "source": [
    "tuple(list(map(float, geolist.replace('POINT (','').replace(')','').split(\" \"))))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "## 文字列をリストに入れる"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## PATH関連"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['', '/usr/local/pyenv/versions/3.6.1/lib/python36.zip', '/usr/local/pyenv/versions/3.6.1/lib/python3.6', '/usr/local/pyenv/versions/3.6.1/lib/python3.6/lib-dynload', '/usr/local/pyenv/versions/jupyter/lib/python3.6/site-packages', '/usr/local/pyenv/versions/jupyter/lib/python3.6/site-packages/IPython/extensions', '/Users/ftakao2007/.ipython', '/tmp', '/tmp', '/tmp', '/tmp', '/tmp', '/tmp', '/tmp']\n",
      "['', '/usr/local/pyenv/versions/3.6.1/lib/python36.zip', '/usr/local/pyenv/versions/3.6.1/lib/python3.6', '/usr/local/pyenv/versions/3.6.1/lib/python3.6/lib-dynload', '/usr/local/pyenv/versions/jupyter/lib/python3.6/site-packages', '/usr/local/pyenv/versions/jupyter/lib/python3.6/site-packages/IPython/extensions', '/Users/ftakao2007/.ipython', '/tmp', '/tmp', '/tmp', '/tmp', '/tmp', '/tmp', '/tmp']\n"
     ]
    }
   ],
   "source": [
    "import sys\n",
    "\n",
    "### デフォルトPATHの確認\n",
    "print(sys.path)\n",
    "\n",
    "### デフォルトPATHの追加\n",
    "### /tmpディレクトリ配下のファイルをインポートできるようになる\n",
    "temp = '/tmp'\n",
    "if not temp in sys.path:\n",
    "    sys.path.append(temp)\n",
    "print(sys.path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "print('/tmp' in sys.path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hoge\n"
     ]
    }
   ],
   "source": [
    "if '/tmp' in sys.path:\n",
    "   print(\"hoge\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'/Users/ftakao2007/work/python/jupyter_notebook/Python'"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import os\n",
    "\n",
    "### カレントディレクトリの確認\n",
    "os.getcwd()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 直列化・非直列化\n",
    "\n",
    "* 直列化 (Serialize) \n",
    "    * オブジェクトをバイト列などの表現に変換すること\n",
    "* 非直列化 (Deserialize)\n",
    "    * バイト列を元のオブジェクトに復元する\n",
    "* pickleをimportして使う\n",
    "* 機械学習で学習済みのモデルを保存して取り回すためなんかに使われる"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello, World!\n"
     ]
    }
   ],
   "source": [
    "import pickle\n",
    "\n",
    "with open('pkl/sample.pkl', mode='wb') as f:\n",
    "    pickle.dump('Hello, World!', f)\n",
    "    \n",
    "with open('pkl/sample.pkl', mode='rb') as g:\n",
    "    print(pickle.load(g))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
